package org.jeecg.modules.product.basics.service.impl;

import org.jeecg.modules.product.basics.entity.TbPeople;
import org.jeecg.modules.product.basics.entity.TbPeopleCertificate;
import org.jeecg.modules.product.basics.entity.TbPeopleEducationTraining;
import org.jeecg.modules.product.basics.entity.TbPeopleRewardsPunishments;
import org.jeecg.modules.product.basics.mapper.TbPeopleCertificateMapper;
import org.jeecg.modules.product.basics.mapper.TbPeopleEducationTrainingMapper;
import org.jeecg.modules.product.basics.mapper.TbPeopleMapper;
import org.jeecg.modules.product.basics.mapper.TbPeopleRewardsPunishmentsMapper;
import org.jeecg.modules.product.basics.service.ITbPeopleService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 人员档案信息
 * @Author: jeecg-boot
 * @Date:   2024-07-05
 * @Version: V1.0
 */
@Service
public class TbPeopleServiceImpl extends ServiceImpl<TbPeopleMapper, TbPeople> implements ITbPeopleService {

	@Autowired
	private TbPeopleMapper tbPeopleMapper;
	@Autowired
	private TbPeopleCertificateMapper tbPeopleCertificateMapper;
	@Autowired
	private TbPeopleEducationTrainingMapper tbPeopleEducationTrainingMapper;
	@Autowired
	private TbPeopleRewardsPunishmentsMapper tbPeopleRewardsPunishmentsMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TbPeople tbPeople, List<TbPeopleCertificate> tbPeopleCertificateList, List<TbPeopleEducationTraining> tbPeopleEducationTrainingList, List<TbPeopleRewardsPunishments> tbPeopleRewardsPunishmentsList) {
		tbPeopleMapper.insert(tbPeople);
		if(tbPeopleCertificateList!=null && tbPeopleCertificateList.size()>0) {
			for(TbPeopleCertificate entity:tbPeopleCertificateList) {
				//外键设置
				entity.setPeopleId(tbPeople.getId());
				tbPeopleCertificateMapper.insert(entity);
			}
		}
		if(tbPeopleEducationTrainingList!=null && tbPeopleEducationTrainingList.size()>0) {
			for(TbPeopleEducationTraining entity:tbPeopleEducationTrainingList) {
				//外键设置
				entity.setPeopleId(tbPeople.getId());
				tbPeopleEducationTrainingMapper.insert(entity);
			}
		}
		if(tbPeopleRewardsPunishmentsList!=null && tbPeopleRewardsPunishmentsList.size()>0) {
			for(TbPeopleRewardsPunishments entity:tbPeopleRewardsPunishmentsList) {
				//外键设置
				entity.setPeopleId(tbPeople.getId());
				tbPeopleRewardsPunishmentsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TbPeople tbPeople,List<TbPeopleCertificate> tbPeopleCertificateList,List<TbPeopleEducationTraining> tbPeopleEducationTrainingList,List<TbPeopleRewardsPunishments> tbPeopleRewardsPunishmentsList) {
		tbPeopleMapper.updateById(tbPeople);
		
		//1.先删除子表数据
		tbPeopleCertificateMapper.deleteByMainId(tbPeople.getId());
		tbPeopleEducationTrainingMapper.deleteByMainId(tbPeople.getId());
		tbPeopleRewardsPunishmentsMapper.deleteByMainId(tbPeople.getId());
		
		//2.子表数据重新插入
		if(tbPeopleCertificateList!=null && tbPeopleCertificateList.size()>0) {
			for(TbPeopleCertificate entity:tbPeopleCertificateList) {
				//外键设置
				entity.setPeopleId(tbPeople.getId());
				tbPeopleCertificateMapper.insert(entity);
			}
		}
		if(tbPeopleEducationTrainingList!=null && tbPeopleEducationTrainingList.size()>0) {
			for(TbPeopleEducationTraining entity:tbPeopleEducationTrainingList) {
				//外键设置
				entity.setPeopleId(tbPeople.getId());
				tbPeopleEducationTrainingMapper.insert(entity);
			}
		}
		if(tbPeopleRewardsPunishmentsList!=null && tbPeopleRewardsPunishmentsList.size()>0) {
			for(TbPeopleRewardsPunishments entity:tbPeopleRewardsPunishmentsList) {
				//外键设置
				entity.setPeopleId(tbPeople.getId());
				tbPeopleRewardsPunishmentsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		tbPeopleCertificateMapper.deleteByMainId(id);
		tbPeopleEducationTrainingMapper.deleteByMainId(id);
		tbPeopleRewardsPunishmentsMapper.deleteByMainId(id);
		tbPeopleMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tbPeopleCertificateMapper.deleteByMainId(id.toString());
			tbPeopleEducationTrainingMapper.deleteByMainId(id.toString());
			tbPeopleRewardsPunishmentsMapper.deleteByMainId(id.toString());
			tbPeopleMapper.deleteById(id);
		}
	}
	
}
